package com.likl.service.impl;

import com.likl.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * ClassName:springboot_study
 * Description:
 *
 * @Create: 2024/7/16 17:13
 * @author: likl
 * @e-mail: likl1102@foxmail.com
 * @QQ: 2514927731
 */
@Service
public class SendMailServiceImpl2 implements SendMailService {

    @Autowired
    private JavaMailSender javaMailSender;

    private String from = "2514927731@qq.com";

    private String to = "likl@fiberhome.com";

    private String subject = "测试邮件";

    private String context = "<a href='http://10.190.156.60'>所得寺内</a>";

    @Override
    public void sendMail() {
        try {
            MimeMessage messages = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(messages, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(context, true);
            File file = new File("D:\\spring_boot\\springboot_study\\springboot_23_mail\\src\\main\\resources\\100.jpg");
            helper.addAttachment(file.getName(), file);
            javaMailSender.send(messages);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}
